﻿using System;
using System.Linq;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class AccountingHistory : Utils.BaseForm
    {
        public AccountingHistory(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            DevComponents.DotNetBar.Controls.GroupPanel.CheckForIllegalCrossThreadCalls = false;
        }

        private void AccountingHistory_Load(object sender, EventArgs e)
        {
            cmbUsers.DisplayMember = "Username";
            cmbUsers.ValueMember = "UserID";
            cmbUsers.DataSource = this.Context.Users;

            cmbEvent.DisplayMember = "AccountingEvent";
            cmbEvent.ValueMember = "AccountingEventID";
            cmbEvent.DataSource = this.Context.AccountingEvents;

            cmbUsers.SelectedIndex = -1;
            cmbEvent.SelectedIndex = -1;
            dtDay.Value = DateTime.Now;

            btnSearch.PerformClick();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            IQueryable<QFlow.Data.AccountingLog> lst = this.Context.AccountingLogs;
            if (cmbUsers.SelectedIndex != -1)
                lst = lst.Where(log => log.UserID == (int)cmbUsers.SelectedValue);
            if (cmbEvent.SelectedIndex != -1)
                lst = lst.Where(log => log.AccontingEventID == (int)cmbEvent.SelectedValue);
            if (dtDay.LockUpdateChecked)
                lst = lst.Where(log => log.TimeStamp.Date == dtDay.Value.Date);

            gvResults.AutoGenerateColumns = false;
            gvResults.DataSource = lst.ToArray();
        }
    }
}
